package com.hyacinth.aurora.constant;

import org.fusesource.jansi.Ansi;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.Random;

/**
 * 相关常量
 * @author zhangfb
 * @since JDK 1.8
 */
public final class Constant {

    private static final Properties SYSTEM_PROPS = new Properties();

    static {
        // 初始化系统参数
        try {
            ClassLoader cl = Constant.class.getClassLoader();
            URL url = (cl != null ? cl.getResource(SystemProperties.SYSTEM_PROPERTIES) :
                    ClassLoader.getSystemResource(SystemProperties.SYSTEM_PROPERTIES));
            if (url != null) {
                try (InputStream is = url.openStream()) {
                    SYSTEM_PROPS.load(is);
                }
            }
        } catch (Throwable e) {
            // ignore
        }
    }

    public final static String AURORA_VERSION = SYSTEM_PROPS.getProperty("aurora.version");
    public final static String AURORA_DESC = SYSTEM_PROPS.getProperty("aurora.description");
    public final static String SERVER_PORT = "server.port";
    public final static String SERVER_CONTEXT_PATH = "server.context.path";
    public final static String SERVER_CHANNEL = "server.channel";
    public final static String UTF_8 = "utf-8";
    public final static String PLUGIN_PACKAGE = "com.hyacinth.aurora.core.plugin";

    public static final class ContentType {
        public final static String JSON = "application/json; charset=UTF-8";
        public final static String TEXT = "text/plain; charset=UTF-8";
        public final static String HTML = "text/html; charset=UTF-8";
        public final static String SET_COOKIE = "Set-Cookie";
        public final static String COOKIE = "Cookie";
    }

    public static final class SystemProperties {

        public static final String LOGO = Logo.genLogo();

        final static String SYSTEM_PROPERTIES = "au.properties";
        public final static String APPLICATION_PROPERTIES = "aurora.properties";
        public final static String APPLICATION_THREAD_MAIN_NAME = "☝( ◠‿◠ )☝";
        public final static String APPLICATION_THREAD_BOOS_NAME = "boos";
        public final static String APPLICATION_THREAD_WORK_NAME = "(♛‿♛)";
        public final static String APPLICATION_THREAD_SHUTDOWN_NAME = "(〒︿〒)";
    }

    public static final class HttpMethod {
        public static final String GET = "GET";
        public static final String POST = "POST";
        public static final String PUT = "PUT";
        public static final String DELETE = "DELETE";
    }

    public static final class Logo {

        static final Ansi.Color[] colors = Ansi.Color.values();

        private static String genLogo() {
            String LOGO = "\n" +
                    "\t\t ▄▄▄       █    ██  ██▀███   ▒█████   ██▀███   ▄▄▄      \n" +
                    "\t\t▒████▄     ██  ▓██▒▓██ ▒ ██▒▒██▒  ██▒▓██ ▒ ██▒▒████▄    \n" +
                    "\t\t▒██  ▀█▄  ▓██  ▒██░▓██ ░▄█ ▒▒██░  ██▒▓██ ░▄█ ▒▒██  ▀█▄  \n" +
                    "\t\t░██▄▄▄▄██ ▓▓█  ░██░▒██▀▀█▄  ▒██   ██░▒██▀▀█▄  ░██▄▄▄▄██ \n" +
                    "\t\t ▓█   ▓██▒▒▒█████▓ ░██▓ ▒██▒░ ████▓▒░░██▓ ▒██▒ ▓█   ▓██▒\n" +
                    "\t\t ▒▒   ▓▒█░░▒▓▒ ▒ ▒ ░ ▒▓ ░▒▓░░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ▒▒   ▓▒█░\n" +
                    "\t\t  ▒   ▒▒ ░░░▒░ ░ ░   ░▒ ░ ▒░  ░ ▒ ▒░   ░▒ ░ ▒░  ▒   ▒▒ ░\n" +
                    "\t\t  ░   ▒    ░░░ ░ ░   ░░   ░ ░ ░ ░ ▒    ░░   ░   ░   ▒   \n" +
                    "\t\t      ░  ░   ░        ░         ░ ░     ░           ░  ░\n" +
                    "\t\t" +AURORA_DESC + "\t\t\t"+ AURORA_VERSION +"\n";

            BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(LOGO.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));

            String line;
            StringBuilder sb = new StringBuilder();
            try {
                while ( (line = br.readLine()) != null ) {
                    if(!line.trim().equals("")){
                        line = decorate(line);
                        sb.append(line).append("\r\n");
                    }
                }
            } catch (IOException e) {
                return LOGO;
            }
            return sb.toString();
        }

        private static String decorate(String line) {
            Ansi.Color color = colors[genRandom()];
            return String.valueOf(Ansi.ansi().eraseScreen().fg(color).a(line).reset());
        }

        private static int genRandom() {
            Random r = new Random();
            return r.nextInt(9);
        }
    }
}
